<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色按钮 -->
    <el-row>
      <el-col>
         <el-button type="primary">添加角色</el-button>
      </el-col>
    </el-row>
    <!-- 角色列表区域 -->
    <el-table
      :data="rolesList">
    <el-table-column type="expand">
      <template slot-scope="props">
        <el-row
        :class="{bottomBorder: i1 === props.row.children.length - 1, topBorder: i1 === 0}"
          v-for="(item1, i1) in props.row.children"
          :key="item1.id"
        >
          <!-- 一级权限 -->
          <el-col :span="5">
            <el-tag>{{item1.authName}}</el-tag>
            <i class="el-icon-caret-right"></i>
          </el-col>
          <!-- 二级和三级权限 -->
          <el-col :span="19">
            <el-row
              v-for="item2 in item1.children"
              :key="item2.id">
               <el-col :span="8">
                 <el-tag type="success">{{item2.authName}}</el-tag>
                 <i class="el-icon-caret-right"></i>
               </el-col>
               <el-col :span="16">
                 <el-row>
                   <el-col>
                     <el-tag
                       style="margin: 5px"
                       v-for="item3 in item2.children"
                       :key="item3.id"
                       type="warning">{{item3.authName}}</el-tag>
                   </el-col>
                 </el-row>
               </el-col>
            </el-row>
          </el-col>
        </el-row>
      </template>
    </el-table-column>
    <el-table-column type="index" label="#"></el-table-column>
    <el-table-column label="角色名称" prop="roleName"></el-table-column>
    <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <!-- 修改 -->
        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
        <!-- 删除 -->
        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        <!-- 分配权限 -->
        <el-button @click="getRolesID(scope.row.id)" type="warning" icon="el-icon-s-tools" size="mini">分配权限</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分配权限对话框 -->
  <el-dialog
    title="分配权限"
    :visible.sync="rolesDialogVisible"
    width="50%">
  <!-- 主体区域 -->
  <el-tree
    ref="newTopRightsTree"
    :data="rightsList"
    show-checkbox
    node-key="id"
    :default-expanded-keys="defaultCheckKeys"
    :default-checked-keys="defaultCheckKeys"
    :props="defaultProps">
  </el-tree>
  <span slot="footer" class="dialog-footer">
    <el-button @click="rolesDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="rolesDialogVisible = false">确 定</el-button>
  </span>
  </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      rolesList: [],
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defaultCheckKeys: [],
      rolesDialogVisible: false
    }
  },
  created () {
    this.getRoleList()
    this.getRightsList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.rolesList = res.data
    },
    // 获取权限列表
    async getRightsList () {
      const { data: res } = await this.$http.get('rights/tree')
      this.rightsList = res.data
    },
    getRolesID (id) {
      this.defaultCheckKeys = []
      this.rolesDialogVisible = true
      this.rolesList.filter(item => item.id === id)[0].children.forEach(element => {
        for (const key in element) {
          if (key === 'children') {
            element[key].forEach(i => {
              this.defaultCheckKeys.push(i.id)
            })
          }
        }
      })
      console.log(this.defaultCheckKeys)
      this.$refs.newTopRightsTree.setCheckedKeys([])
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  margin-top: 10px;
  margin-bottom: 10px;

  display: flex;
  align-items: center;
}

.topBorder {
  border-top: 1px solid #ccc;
}
.bottomBorder {
  border-bottom: 1px solid #ccc;
}
</style>
